using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presentation.Contracts;
using Presentation;
using DataAccess;
using System.Collections.Generic;
using Globals;
using System.IO;

namespace ProjektZespolowy
{
    public partial class UserLogInGallery : Page, IUserLogInGalleryView
    {
        private int _galleryId = 0;

        /// <summary>
        /// Presenter.
        /// </summary>
        private UserLogInGalleryPresenter presenter;

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new UserLogInGalleryPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            FillPage();
        }

        private void FillPage()
        {
            string idString = Request.QueryString["id"];
            int.TryParse(idString, out _galleryId);

            List<art> fotoArts = presenter.ReadAllFotoArtForUserForGallery();
            RepeaterFoto.DataSource = fotoArts;
            RepeaterFoto.DataBind();

            List<art> textArts = presenter.ReadAllTextArtForUserForGallery();
            RepeaterText.DataSource = textArts;
            RepeaterText.DataBind();

            List<art> binaryArts = presenter.ReadAllBinaryArtForUserForGallery();
            RepeaterBinary.DataSource = binaryArts;
            RepeaterBinary.DataBind();

            if (int.TryParse(idString, out _galleryId))
            {
                //w url poprawny int
                if(!Page.IsPostBack)
                    presenter.ReadGalleryByGalleryId();

                if (!Page.IsPostBack)
                {
                    presenter.ReadArtTypes();
                    lblArtType.Text = "Tekst";
                }
            }
            else
            {
                DisplayError("Podano niepoprawny numer galerii");
            }
        }

        public int GalleryId
        {
            get
            {
                return _galleryId;
            }
        }

        public int UserId
        {
            get
            {
                //return ContentNamesGlobals.hudymaUserId; 
                if (Session[ContentNamesGlobals.SessionWithUserId] == null)
                    return 0;
                return int.Parse((string)Session[ContentNamesGlobals.SessionWithUserId]);
            }
        }

        public void DisplayError(string error)
        {
            mvwPage.SetActiveView(vwError);
            lblError.Text = error;
        }

        public void DisplayGalleria(gallery galleryTmp)
        {
            mvwPage.SetActiveView(vwPage);
            lblGalleryName.Text = galleryTmp.name;
            lblGalleryDescription.Text = galleryTmp.description;
        }

        public void DisplayArtTypes(List<art_type> artTypes)
        {
            ddlArtType.DataSource = artTypes;
            ddlArtType.DataValueField = "id";
            ddlArtType.DataTextField = "name";
            ddlArtType.DataBind();
        }

        protected void btnAddArt_Click(object sender, EventArgs e)
        {
            if ((int.Parse(ddlArtType.SelectedValue) == (int)ContentNamesGlobals.ArtType.grafika)
                && FileUploadPhoto.HasFile)
            {
                //FileUploadPhoto.FileContent
            }
            else if ((int.Parse(ddlArtType.SelectedValue) == (int)ContentNamesGlobals.ArtType.binaria)
                && FileUploadBinary.HasFile)
            {
            }
            
            presenter.AddArt();
            FillPage();
            txtText.Text = "";
            txtTytul.Text = "";
            txtOpis.Text = "";

            //FileUploadPhoto.
            //FileUploadBinary

        }

        public string Tytul
        {
            get
            {
                return txtTytul.Text.Trim();
            }
        }

        public ContentNamesGlobals.ArtType Typ
        {
            get
            {
                return (ContentNamesGlobals.ArtType)int.Parse(ddlArtType.SelectedValue);
            }
        }

        public string Text
        {
            get
            {
                return txtText.Text.Trim();
            }
        }

        public string Description
        {
            get
            {
                return txtOpis.Text.Trim();
           } 
        }

        public bool getPhoto(out string name,out Stream stream)
        {
            if (FileUploadPhoto.HasFile)
            {
                name = FileUploadPhoto.FileName;
                stream = FileUploadPhoto.FileContent;
            }
            else
            {
                name = string.Empty;
                stream = null;
            }
            return FileUploadPhoto.HasFile;
        }
        public bool getBinary(out string name, out Stream stream)
        {
            if (FileUploadBinary.HasFile)
            {
                name = FileUploadBinary.FileName;
                stream = FileUploadBinary.FileContent;
            }
            else
            {
                name = string.Empty;
                stream = null;
            }
            return FileUploadBinary.HasFile;
        }


        public void SavePhotoAs(string newFileName)
        {
            FileUploadPhoto.SaveAs(MapPath( newFileName ) );
        }

        public void SaveBinaryAs(string newFileName)
        {
            FileUploadPhoto.SaveAs(MapPath(newFileName) );
        }

        public void DisplayAddPhotoError(string error)
        {
            lblAddPhotoError.Text = error;
        }

        public void DisplayAddBinaryError(string error)
        {
            lblAddBinaryError.Text = error;
        }

        public void CleanAddArtErrors()
        {
            lblAddPhotoError.Text = string.Empty;
            lblAddBinaryError.Text = string.Empty;
        }

        public void CleanAfterAdd(string text)
        {
        }

        public string Tag
        {
            get
            {
                return txtTag.Text.Trim();
            }
            set
            {
                txtTag.Text = value;
            }
        }
        protected void btnAddTag_Click(object sender, EventArgs e)
        {
            presenter.AddTag();
        }

        public void AddTagToSession(string tagName)
        {
            List<string> tags = (List<string>)Session[ContentNamesGlobals.TagsSessionKey];
            if (tags == null)
                tags = new List<string>();
            if (!tags.Contains(tagName))
                tags.Add(tagName);
            Session[ContentNamesGlobals.TagsSessionKey] = tags;
        }

        public void DeleteTagFromSession(string tagName)
        {
            List<string> tags = (List<string>)Session[ContentNamesGlobals.TagsSessionKey];
            if (tags == null)
                return;
            if (tags.Contains(tagName))
                tags.Remove(tagName);
            Session[ContentNamesGlobals.TagsSessionKey] = tags;
        }

        public void CleanSessionFromTags()
        {
            if (Session[ContentNamesGlobals.TagsSessionKey] != null)
                Session[ContentNamesGlobals.TagsSessionKey] = null;
        }

        public List<string> GetTagsFromSession()
        {
            return (List<string>)Session[ContentNamesGlobals.TagsSessionKey];
        }

        public void DisplayTags()
        {
            List<string> tags = (List<string>)Session[ContentNamesGlobals.TagsSessionKey];
            if (tags == null)
                tags = new List<string>();
            repTags.DataSource = tags;
            repTags.DataBind();
        }

        public void DisplayAllTags(List<tag> tags)
        {
            ddlTags.DataSource = tags;
            ddlTags.DataTextField = "name";
            ddlTags.DataValueField = "id";
            ddlTags.DataBind();
        }

        protected void MyDeleteButton_Command(object sender, CommandEventArgs e)
        {
            string tagName = (string)e.CommandArgument;
            presenter.DeleteTag(tagName);
        }

        protected void FotoDeleteButton_Command(object sender, CommandEventArgs e)
        {
            string artIdString = (string)e.CommandArgument;
            int artId = int.Parse(artIdString);
            presenter.DeleteArt(artId);
            FillPage();
        }

        protected void ddlTags_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTag.Text = ddlTags.SelectedItem.ToString();
        }

        public void SavaImageAs(System.Drawing.Image image, string path)
        {
            image.Save(MapPath(path));
        }

        public string GetMiniFoto(string bigFoto)
        {
            string extension = Path.GetExtension(bigFoto);
            bigFoto = bigFoto.Replace(extension, string.Empty);
            return bigFoto + "_mini" + extension;
        }



    }
}
